/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import bean.*;

/**
 *
 * @author root
 */
@Repository
public class ProductDao {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    /**
     * 得到全部产品展示数据
     * @return 
     */
    public List<Product> getListAllProduct(){
        List<Product> list = entityManager.createNativeQuery("select * from product where proid in (1,2,3) order by proId",Product.class).getResultList();
        return list;
    }
    
     /**
      * 分页取得产品信息
      * @param protype 产品类别
      * @param pagesize 每页数量
      * @param page 页数
      * @return 
      */
    public List<Product> getListPagingProductByProTypePage(int protype,int pagesize,int page){
        List<Product> list = null;
        list = entityManager.createNativeQuery("select * from product where protype = "+protype+" order by proId limit "+pagesize+" offset "+pagesize*(page-1),Product.class).getResultList();
        return list;
    }
    
     /**
      * 刷新取得产品信息
      * @param protype
      * @param begin  开始条数
      * @param readsize      读取条数
      * @return 
      */
     public List<Product> getListProductByProTypeRefresh(int protype,int begin,int readsize){
         List<Product> list = null;
         String sql = "select * from product where protype = "+protype+" order by proId limit "+begin+" , "+readsize;
         list = entityManager.createNativeQuery(sql,Product.class).getResultList();
         return list;
     }
     
}
